package member

import (
	"context"
	"errors"

	"group-buy/group_buy/internal/models"
	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetByOpendidLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetByOpendidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetByOpendidLogic {
	return &GetByOpendidLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetByOpendidLogic) GetByOpendid(req *types.GetByOpendIdReq) (resp *types.Member, err error) {

	m := &models.MemberInfo{Member: models.Member{OpenId: req.OpenId}}
	get, err := l.svcCtx.Engine.Join("left", "ls_user_info", "member.ls_user=ls_user_info.channel_user").Join("left", "ls_card_info", "member.ls_user=ls_card_info.card_id").Get(m)

	// get, err := l.svcCtx.Engine.Get(m)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("会员不存在")
	}

	return &types.Member{
		Id:         m.Member.Id,
		Name:       m.Member.Name,
		Phone:      m.Phone,
		Cover:      m.Cover,
		OpenId:     m.OpenId,
		LsCard:     m.CardNo,
		LsUser:     m.LsUserInfo.CertNo,
		Role:       m.Role,
		BusinessId: m.BusinessId,
		CardNo:     m.CardNo,
		CertNo:     m.LsUserInfo.CertNo,
	}, nil
}
